#!/usr/bin/python3
# -*- coding: utf-8 -*-
from typing import Any

import allure
from playwright.sync_api import Page

from utils.log import log


class Popout:
    def __init__(self, page: Page):
        self.page = page

    @allure.step("弹框：点击[提交]按钮")
    def click_submit_by_locator(self):
        """弹框组件-点击提交"""
        self.page.locator("#modalId").locator('[type="submit"]').click()

    @allure.step("弹框：点击[提交]按钮")
    def click_submit_by_role_button(self):
        """弹框组件-点击提交"""
        self.page.get_by_role("button", name="提交").click()

    @allure.step("弹框：点击[提交]按钮")
    def click_submit_by_role_link(self):
        """弹框组件-点击提交"""
        self.page.get_by_role("link", name="提交").click()

    @allure.step("弹框：点击[取消]按钮")
    def click_cancel(self):
        """弹框组件-点击取消"""
        self.page.locator("#modalId").locator('[type="button"]').click()

    @allure.step("弹框：点击[关闭]按钮")
    def click_close(self):
        """弹框组件-点击关闭弹框"""
        self.page.locator("#modalId").locator('[class="close"]').click()

    @allure.step("弹框：点击[确认]按钮")
    def click_alert_confirm_button(self):
        """弹框组件-点击确认按钮"""
        self.page.locator("#modalId").locator('[type="submit"]').click()

    @allure.step("弹框：点击[确认]按钮")
    def click_alert_confirm_button_by_role_button(self):
        """弹框组件-点击确认按钮"""
        self.page.get_by_role("button", name="确认").click()

    @allure.step("弹框：点击[确认]按钮")
    def click_alert_confirm_button_by_role_link(self):
        """弹框组件-点击确认按钮"""
        self.page.get_by_role("link", name="确认").click()

    @allure.step("弹框：点击[取消]按钮")
    def click_cancel_button_by_role_link(self):
        """弹框组件-点击取消按钮"""
        self.page.get_by_role("link", name="取消").click()

    @allure.step("弹框：点击[取消]按钮")
    def click_cancel_button_by_role_button(self):
        """弹框组件-点击取消按钮"""
        self.page.get_by_role("button", name="取消").click()

    @allure.step("弹框：点击[我知道了,确认继续]按钮")
    def click_iknow_and_resume_button_by_role_link(self):
        """弹框组件-点击我知道了,确认继续按钮"""
        self.page.get_by_role("link", name="知道了，确认继续").click()

    @allure.step("弹框：点击[重新填写]按钮")
    def click_fill_in_again_button_by_role_link(self):
        """弹框组件-点击重新填写按钮"""
        self.page.get_by_role("link", name="重新填写").click()

    @allure.step("弹框：点击[导入]按钮")
    def click_import(self):
        """弹框组件-点击导入按钮"""
        self.page.locator("#modalId").locator('[type="submit"][value="导入"]').click()

    @allure.step("弹框：点击[取消]按钮")
    def click_cancel_button(self):
        """弹框组件-点击取消按钮"""
        self.page.locator("#modalId").locator('[type="submit"][value="取消"]').click()

    @allure.step("弹框：点击[继续申请公网 IP ]")
    def click_continue_to_allocate_eip_by_role_link(self):
        self.page.get_by_role("link", name="继续申请公网 IP ").click()

    @allure.step("弹框：点击[继续申请 IPv6 地址]")
    def click_continue_to_allocate_eipv6_by_role_link(self):
        self.page.get_by_role("link", name="继续申请 IPv6 地址").click()

    def get_alert_warning_text_content(self):
        """弹框组件-获取弹框警告内容文本"""
        if self.page.locator("#modalId").locator(".alert-warning").is_visible():
            warning_text_content = (
                self.page.locator("#modalId").locator(".alert-warning").text_content()
            )
            log.info(f"告警文本:{warning_text_content}")
            return warning_text_content

    def get_alert_text_content(self):
        """弹框组件-获取弹框内容文本"""
        if self.page.locator("#modalId").locator(".alert-info").is_visible():
            alert_text_content = (
                self.page.locator("#modalId").locator(".alert-info").text_content()
            )
            log.info(f"弹框文本:{alert_text_content}")
            return alert_text_content

    @allure.step("弹框：点击[确认]按钮")
    def click_alert_confirm_button_if_element_visible(self):
        """弹框组件-确认"""
        if self.page.locator("#modalId").locator(".alert-info").is_visible():
            self.click_alert_confirm_button_by_role_button()

    def click_alert_warning_button_if_element_visible(self):
        """弹框组件-如果警告信息存在则点击"""
        if self.page.locator("#modalId").locator(".alert-warning").is_visible():
            self.click_iknow_and_resume_button_by_role_link()
